//
//  NSDictionary+ZLCategory.m
//  ZLCategory
//
//  Created by zhaozl on 2022/3/7.
//

#import "NSDictionary+ZLCategory.h"

#define isValidKey(key) ((key) != nil && ![key isKindOfClass:[NSNull class]])
#define isValidValue(value) (((value) != nil) && ![value isKindOfClass:[NSNull class]])

@implementation NSDictionary (ZLCategory)

- (NSData *)zl_JSONData {
    return [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
}

- (NSString *)zl_JSONString {
    return [[NSString alloc] initWithData:[self zl_JSONData] encoding:NSUTF8StringEncoding];
}

- (id)zl_safeObjectForKey:(id)key {
    if (!isValidKey(key)) {
        return nil;
    }
    
    id obj = [self objectForKey:key];
    if(!isValidValue(obj)) {
        return nil;
    }
    return obj;
}

@end
